import {WeaTools, WeaLocaleProvider} from 'ecCom';
import {observable, action, toJS, runInAction} from 'mobx';
import * as urls from '../constants/ServerUrls';
import SearchFavourite from '../util/SearchFavourite';


const getLabel = WeaLocaleProvider.getLabel;
const {callApi} = WeaTools;

export default class SysFavouriteStore {
    /** 收藏列表的数据 */
    @observable data = [];
    /** 收藏类型下拉框选中的值 */
    @observable favtype = '';
    /** 列表选择的数据 */
    @observable selectedIds = [];
    /** 移动操作时的id */
    @observable moveids = [];
    /** 操作过程中的遮罩 */
    @observable spinning = false;
    /** 提示信息 */
    @observable tip = '';
    @observable sharedata = {};
    @observable shareShow = false;

    /**
     * 点击选中的一行数据的id
     * @type
     */
    @observable clickItemId = 0;

    /**
     * FavouriteBrowserStore的实例
     */
    favouriteBrowserStore = null;

    /**
     * FavouriteDialogStore的实例
     */
    favouriteDialogStore = null;

    constructor(favouriteBrowserStore, favouriteDialogStore) {
        this.favouriteBrowserStore = favouriteBrowserStore;
        this.favouriteDialogStore = favouriteDialogStore;
    }

    /**
     * 加载收藏夹列表
     * @param params
     */
    @action loadFavouriteList(params) {
        const listurl = urls.LOAD_FAVOURITE_LIST;
        callApi(listurl, 'POST', params).then((resdata) => {
            SearchFavourite.setComplete(true);
            this.setPageData(resdata);
            this.toggleSpinning(false);
        });
    }

    /**
     * 重新加载收藏夹列表
     * @param params
     */
    @action reloadFavouriteList(params) {
        const listurl = urls.LOAD_FAVOURITE_LIST;
        this.toggleSpinning(true, `${getLabel(82275, '数据加载中，请稍候...')}`);

        callApi(listurl, 'POST', params).then((resdata) => {
            SearchFavourite.setComplete(true);
            this.setReloadData(resdata);
            this.toggleSpinning(false);
        });
    }

    /**
     * 设置选中:查询条件中的，收藏类型下拉框选中
     * @param favtype
     */
    @action setFavouriteType(favtype) {
        this.favtype = favtype;
    }

    /**
     * check框选中：收藏列表的check框选中
     * @param itemid
     * @param checked
     */
    @action setCheckedId(itemid, checked) {
        let selectedIds = toJS(this.selectedIds);
        const tempIds = [];

        if (!checked) { // 取消选中
            if (selectedIds.indexOf(itemid) != -1) {
                selectedIds.forEach((sid) => {
                    if (sid != itemid) {
                        tempIds.push(sid);
                    }
                });
            }
            selectedIds = [...tempIds];
        } else if (selectedIds.indexOf(itemid) == -1) {
            selectedIds.push(itemid);
        }

        this.selectedIds = selectedIds;
    }

    /**
     * 每一行收藏记录的状态：鼠标移入、移出
     * @param itemid
     * @param isover
     */
    @action setItemStatus(itemid, isover) {
        const data0 = toJS(this.data);
        data0.forEach((map) => {
            if (Object.is(map.favid, itemid)) { // 当前一条记录，是否鼠标移入、移出
                map.isover = isover;
            }
        });
        this.data = [...data0];
    }

    /**
     * 每一行记录的选中状态设置
     * @param itemid
     * @param isselected
     */
    @action setItemSelect(itemid, isselected) {
        /*this.data.forEach((map) => {
            if (Object.is(map.favid, itemid)) { // 当前记录，是否鼠标选中
                map.isselected = isselected;
            } else {
                map.isselected = false;
            }
        });*/

        this.clickItemId = itemid;
    }

    /**
     * 按钮的鼠标移入、移出
     * @param itemid
     * @param attr
     * @param isover
     */
    @action setBtnOver(itemid, attr, isover) {
        this.data.forEach((map) => {
            if (Object.is(map.favid, itemid)) {
                map[attr] = isover;
            }
        });
    }

    /**
     * 重要程度按钮鼠标移入、移出
     * @param itemid
     * @param isover
     */
    @action setLevelOver(itemid, isover) {
        this.data.forEach((map) => {
            if (Object.is(map.favid, itemid)) {
                map.islevelover = isover;
            }
        });
    }

    /**
     * 修改重要程度
     * @param favid
     * @param importlevel
     */
    @action changeLevel(favid, importlevel) {
        const levelurl = urls.EDIT_IMPORTANT_LEVEL;
        const params = {favid, importlevel};

        this.toggleSpinning(true, `${getLabel(20240, '数据处理中，请稍候...')}`);

        callApi(levelurl, 'POST', params).then((resdata) => {
            const {success} = resdata;
            if (success == '1') {
                const isinit = false;
                const isreload = true;
                // 刷新收藏夹列表，之所以刷新，是因为列表的排序，跟重要程序相关
                const searchFavourite = new SearchFavourite(this);
                const params = {isinit, isreload};
                searchFavourite.load(params);
            }
        });
    }

    /**
     * 删除收藏
     * @param favid
     */
    @action deleteFavourite(favid) {
        const favurl = urls.DELETE_FAVOURITE;
        const params = {favid};

        this.toggleSpinning(true, `${getLabel(20240, '数据处理中，请稍候...')}`);

        callApi(favurl, 'POST', params).then((resdata) => {
            const {success} = resdata;
            if (success == '1') {
                const isinit = false;
                const isreload = true;
                // 刷新收藏夹列表
                const searchFavourite = new SearchFavourite(this);
                const params = {isinit, isreload};
                searchFavourite.load(params);
            }
        });
    }

    /**
     * 设置待移动的收藏内容的id
     * @param moveids
     */
    @action setMoveIds(moveids) {
        this.moveids = moveids;
    }

    /**
     * 移动收藏条目到另一目录
     * @param favids
     * @param dirid
     * @returns {function(*=)}
     */
    @action moveToAnotherDir(favids, dirid) {
        const moveurl = urls.MOVE_FAVOURITES;
        const params = {favids, dirid};
        this.toggleSpinning(true, `${getLabel(20240, '数据处理中，请稍候...')}`);
        callApi(moveurl, 'POST', params).then((resdata) => {
            const {success} = resdata;
            if (success == '1') {
                // 关闭收藏目录的弹出窗口
                if (this.favouriteDialogStore) {
                    this.favouriteDialogStore.closeBrowserDialog();
                }

                // 清空窗口的数据
                if (this.favouriteBrowserStore) {
                    this.favouriteBrowserStore.destroyBrowser();
                }

                // 刷新收藏夹列表
                const isinit = false;
                const isreload = true;
                const searchFavourite = new SearchFavourite(this);
                const params2 = {isinit, isreload};
                searchFavourite.load(params2);
            }
        });
    }

    /**
     * 添加文档、流程、项目、客户的收藏，收藏的是具体的id
     * @param ids
     * @param dirid
     * @param names
     * @param favouritetype
     * @returns {function(*=, *)}
     */
    @action addSysFavourites(ids, dirid, names, favouritetype) {
        if (dirid == '0') { // 这是选择了根目录，默认将收藏的内容，放到“我的收藏”
            dirid = '-1';
        }
        const importantlevel = 1; // 紧急程度：正常

        const favurl = urls.ADD_FAVOURITES;
        const params = {ids, names, dirid, importantlevel, favouritetype};

        this.toggleSpinning(true, `${getLabel(20240, '数据处理中，请稍候...')}`);

        callApi(favurl, 'POST', params).then((resdata) => {
            // 刷新收藏夹列表
            const isinit = true;
            const isreload = false;
            const searchFavourite = new SearchFavourite(this);
            const params2 = {isinit, isreload};
            searchFavourite.load(params2);
        });
    }

    /**
     * 隐藏或者显示遮罩
     * @param spinning
     * @param tip
     */
    @action
    toggleSpinning = (spinning, tip = '') => {
        this.spinning = spinning;
        this.tip = tip;
    }

    /**
     * 设置分页数据，将新加载的数据，合并到原有的数据中
     * @param data
     */
    @action.bound
    setPageData(data) {
        const data0 = this.data;
        const {count, maxId, total, current} = data;
        const data1 = data.data;
        SearchFavourite.setConditions({count, maxId, total, current}); // 缓存分页的信息

        runInAction(() => {
            this.data = [...data0, ...data1];
            this.clickItemId = 0;
        });
    }

    /**
     * 设置重载的数据
     * @param data
     */
    @action.bound
    setReloadData(data) {
        const {count, maxId, total, current} = data;
        const data0 = data.data;
        SearchFavourite.setConditions({count, maxId, total, current}); // 缓存分页的信息
        runInAction(() => {
            this.selectedIds = []; // 初始化已选中的数据
            this.data = [...data0];
            this.clickItemId = 0;
        });
    }
}
